# -*- coding: utf-8 -*-
"""
Created on Tue Nov 22 14:44:22 2022

@author: lenovo
"""
import numpy as np
from sklearn.manifold import TSNE
import matplotlib.pyplot as plt
import math
import numpy as np
from sklearn.metrics.pairwise import cosine_similarity, paired_distances


# %%

def traintsne(data, label):
    font1 = {'family': 'Times New Roman',
             'weight': 'normal',
             'size': 10, }
    ts = TSNE(n_components=2, init='pca', random_state=0)
    feature = ts.fit_transform(data)
    plt.figure(0, figsize=[6, 5])
    for i in range(len(np.unique(label))):
        temp = feature[label[:, 0] == i]
        # temp2 = feature[label[:,0]==1]
        # temp3 = feature[label[:,0]==2]
        # temp4 = feature[label[:,0]==3]
        plt.plot(temp[:, 0], temp[:, 1], 'o')
    # plt.plot(temp2[:,0],temp2[:,1],'o',color = 'orange')
    # plt.plot(temp3[:,0],temp3[:,1],'o',color = 'black')
    # plt.plot(temp4[:,0],temp4[:,1],'o',color = 'green')
    #    plt.plot(feature[label[:,0]==1, 0], feature[label[:,0]==1, 1],'o',color = 'blue')
    #    plt.plot(feature[label[:,0]==0, 0], feature[label[:,0]==0, 1],'o',feature[label[:,0]==1, 0], feature[label[:,0]==1, 1], 'o',
    #             feature[label[:,0]==2, 0], feature[label[:,0]==2, 1],'o',feature[label[:,0]==3, 0], feature[label[:,0]==3, 1],'o')
    # plt.legend(["F1","F2","F3","F4"],prop=font1)
    plt.yticks(fontproperties='Times New Roman', size=10)
    plt.xticks(fontproperties='Times New Roman', size=10)
    plt.xlabel("Dimension1", fontproperties='Times New Roman', size=10)
    plt.ylabel("Dimension1", fontproperties='Times New Roman', size=10)
    plt.show()


# %%

# return temp1, temp2, temp3, temp4
def getcos(x1, y1, data):
    dis = []
    idx = []
    for i in range(0, len(data)):
        for j in range(i + 1, len(data)):
            temp = cosine_similarity((y1 - x1), (data[j:j + 1, :] - data[i:i + 1, :])[:, 0:1024])
            dis.append(temp[0])
            idx.append([i, j])
    dis = np.abs(np.array(dis).reshape(-1, 1))
    index = np.argmax(dis)
    return dis, idx, index, dis[index], data[idx[index][0]:idx[index][0] + 1, :], data[idx[index][1]:idx[index][1] + 1, :]


# %%
# a = np.array([[-1, 1]])
# b = np.array([[1, -1]])
# sim = cosine_similarity(a, b)
def getfinals(x1, y1, n, all_data, n_cluster):
    id1 = [i for i in range(0, n_cluster)]
    result = []
    cos_max = []
    for i in id1:
        if i != n:
            temp_r = getcos(x1, y1, all_data[i])
            result.append(temp_r)
            cos_max.append(temp_r[3][0])
    index = np.argmax(np.array(cos_max).reshape(-1, 1))
    x2, y2 = result[index][len(result[index]) - 2:len(result[index])]

    return cos_max, index, np.vstack((x2, y2))


def getsamplepair(data):
    sample = []
    for i in range(0, len(data)):
        for j in range(i+1, len(data)):
            x1 = data[i:i+1, 0:data.shape[1] - 1]
            y1 = data[j:j+1, 0:data.shape[1] - 1]
            temp = np.concatenate([x1, y1], axis=1)
            sample.append(temp)
    return np.concatenate(sample)

def getallsamplepair(data):
    sample = []
    for i in range(len(data)):
        sample.append(getsamplepair(data[i]))
    return sample

def getfinalltrain(data):
    finall_r = []
    for i in range(0, len(data)):
        for j in range(0, len(data)):
          if i!=j:
            temp_x = data[i]
            temp_y = data[j]
            # print(len(temp_x))
            # print(len(temp_y))
            length = np.arange(min(len(temp_x), len(temp_y)))
            np.random.shuffle(length)
            temp_x = temp_x[length,:]
            np.random.shuffle(length)
            temp_y = temp_y[length,:]
            result = np.concatenate([temp_x, temp_y],axis=1)
            finall_r.append(result)
    return  finall_r

def zero_average(sample):
    # print(1)
    # print(sample.shape[0])
    for i in range(sample.shape[0]):
        sample[i,:] = sample[i,:] - np.mean(sample[i,:])
    return sample
#%%
# from sklearn import preprocessing
# import numpy as np

# x = np.array([[1.,-1.,2.],
#  [2.,0.,0.],
#  [0.,1.,-1.]])

# min_max_scaler = preprocessing.MinMaxScaler()#默认为范围0~1，拷贝操作

# #min_max_scaler = preprocessing.MinMaxScaler(feature_range = (1,3),copy = False)#范围改为1~3，对原数组操作

# x_minmax = min_max_scaler.fit_transform(x)




